package com.oracle.lingshangejia.base;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.WindowManager;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.dialog.LoadingViewDialog;
import com.oracle.lingshangejia.utils.AppManagerDelegate;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.PhoneStateUtils;
import com.tencent.bugly.crashreport.CrashReport;


/**
 * Created by Sl on 2018/12/28.
 * activity基类
 */

public abstract class BaseActivity extends FragmentActivity {
    private LoadingViewDialog mLoadingView;
    private int netMobile;
    private ImmersionBar mImmersionBar;
    /***获取TAG的activity名称**/
    protected final String TAG = this.getClass().getSimpleName();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//保持屏幕亮
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        //设置布局
        setContentView(intiLayout());
        checkNetworked();
        AppManagerDelegate.getInstance().addActivity(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar
                .barColor(R.color.colorBlue)
                .fitsSystemWindows(true).init();   //所有子类都将继承这些相同的属性
        //初始化控件
        initView();
        //设置数据
        initData();

    }

    protected boolean checkNetworked() {
        if (!PhoneStateUtils.checkNetwork(this)) {
            MyToast.show(getApplicationContext(), "手机无可用网络！");
            return false;
        }
        return true;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null)
            mImmersionBar.destroy();  //必须调用该方法，防止内存泄漏，不调用该方法，如果界面bar发生改变，在不关闭app的情况下，退出此界面再进入将记忆最后一次bar改变的状态
    }
    /**
     * 设置布局
     *
     * @return
     */
    public abstract int intiLayout();

    /**
     * 初始化布局
     */
    public abstract void initView();

    /**
     * 设置数据
     */
    public abstract void initData();

    /**
     * 显示长toast
     * @param msg
     */
    public void toastLong(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
    }

    /**
     * 显示短toast
     * @param msg
     */
    public void toastShort(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }


    public void showLoading(){
        try {
            mLoadingView = new LoadingViewDialog();
            mLoadingView.show(getSupportFragmentManager(), "");
        }catch (Exception e) {
            CrashReport.postCatchedException(e);
            com.lws.basic.utils.common.MyToast.show(e.getMessage());
        }

    }
    public void dismissLoading(){
        try {
            if (mLoadingView != null && mLoadingView.isAdded()) {
                mLoadingView.dismiss();
            }
        }catch (Exception e) {
            CrashReport.postCatchedException(e);
            com.lws.basic.utils.common.MyToast.show(e.getMessage());
        }

    }

}
